﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Sales;
using NCP.Recruiting.WebApp.Models.Deals;
using NCP.Recruiting.WebApp.Models.Contracts;

namespace NCP.Recruiting.WebApp.Controllers {
    public class DealController : AuthorizedController {
        private readonly ICompanyAppService _companyAppService;
        private readonly ISalesAppService _salesAppService;

        public DealController(ICompanyAppService companyAppService,
            ISalesAppService salesAppService)
            : base(companyAppService) {
            this._companyAppService = companyAppService;
            this._salesAppService = salesAppService;
        }


        public ActionResult Deals(DealPagingFilteringModel command) {
            var model = new DealListModel();

            if (command.PageSize <= 0)
                command.PageSize = 10;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            GetDealsByCompanyRequest request = new GetDealsByCompanyRequest(CurrentUserId);
            request.PageIndex = command.PageNumber - 1;
            request.PageCount = command.PageSize;
            GetDealsByCompanyResponse response = _salesAppService.GetDealsByCompany(request);

            model.PagingFilteringContext.LoadPagedList(response.PagedDeals);

            model.Deals = response.PagedDeals;


            return View(model);
        }

        public ActionResult Contracts(ContractPagingFilteringModel command) {
            var model = new ContractListModel();

            if (command.PageSize <= 0)
                command.PageSize = 10;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            var request = new GetContractsByCompanyRequest(CurrentUser.Id);
            request.PageIndex = command.PageNumber - 1;
            request.PageCount = command.PageSize;
            var response = _salesAppService.GetContractsByCompany(request);

            model.PagingFilteringContext.LoadPagedList(response.PagedContracts);
            model.Contracts = response.PagedContracts;

            return View(model);
        }

    }
}
